package threading;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xwl
 * Date: 2023-08-11
 * Time: 21:56
 */
public class ThreadDemo23 {
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger num = new AtomicInteger(0);

        Thread t1 = new Thread( () -> {
           for (int i = 0; i < 50000; i++) {
               // num++
               // 利用CAS
               num.getAndIncrement();
           }
        });

        Thread t2 = new Thread( () -> {
           for (int i = 0; i < 50000; i++) {
               num.getAndIncrement();
           }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(num);
    }
}
